<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class category extends core {
	public function __construct() {
		parent::__construct();
	}

	//分类首页
	public function index(){
		$category = $this->get_categorys();
		$category = add_domain_deep($category, array("pic"));
		
		success(array('category'=>$category));
	}
	
	private function get_categorys($parent_id=0) {
		$sql = "SELECT *, NULL as children FROM {$this->tbp}goods_category 
			WHERE status='1' AND parent_id='{$parent_id}' ORDER BY sort ASC, id ASC";
		$category = $this->db->get_results($sql);
		if ($category) {
			foreach ($category as $k => $g) {
				$category[$k]->flashes = $this->_flashes($g->id);
				$sql = "SELECT count(*) FROM {$this->tbp}goods_category WHERE parent_id='{$g->id}'";
				if ($this->db->get_var($sql)) $category[$k]->children = $this->get_categorys($g->id);
			}
		}
		return $category;
	}

	//幻灯广告
	private function _flashes($id) {
		$sql = "SELECT * FROM {$this->tbp}ad WHERE status='1' AND position='category{$id}' ORDER BY sort ASC, id DESC LIMIT 5";
		$ads = $this->db->get_results($sql);
		if ($ads) {
			$ads = add_domain_deep($ads, array("pic"));
		}
		return $ads;
	}
}
